package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.ChatActivity;
import com.beaven.imclient.adapter.UserAdapter;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.data.ItemCategory;
import com.beaven.imclient.data.ItemUser;
import com.beaven.imclient.presenter.user.UserContract;
import com.beaven.imclient.presenter.user.UserPresenter;
import com.beaven.imclient.utils.ToastUtil;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

/**
 * A simple {@link Fragment} subclass.
 */
public class FriendFragment extends BaseFragment implements UserContract.View {

  public static FriendFragment newInstance() {
    return new FriendFragment();
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_friend;
  }

  private UserAdapter adapter;
  private UserContract.Presenter presenter;
  private Handler handler = new Handler();
  private ProgressDialog dialog;

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    RecyclerView recycler_user = (RecyclerView) rootView.findViewById(R.id.recycler_user);

    adapter = new UserAdapter(new ArrayList<MultiItemEntity>(), getContext());
    adapter.setListener(listener);
    recycler_user.setAdapter(adapter);
    recycler_user.setLayoutManager(new LinearLayoutManager(this.getContext()));
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new UserPresenter(this));
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if (hidden) {
      presenter.unSubscribe();
    } else {
      presenter.subscribe();
    }
  }

  private void expand(List<MultiItemEntity> dataList) {
    for (int i = 0; i < dataList.size(); i++) {
      MultiItemEntity entity = dataList.get(i);
      if (entity.getItemType() == UserAdapter.TYPE_TITLE) {
        adapter.expand(i);
      }
    }
  }

  private UserAdapter.ItemTouchListener listener = new UserAdapter.ItemTouchListener() {
    @Override public void onItemClick(MultiItemEntity entity) {
      if (entity.getItemType() == UserAdapter.TYPE_USER) {
        ItemUser itemUser = (ItemUser) entity;
        ChatActivity.showUser(getContext(), itemUser.getId());
      } else if (entity.getItemType() == UserAdapter.TYPE_CATEGORY) {
        ItemCategory itemCategory = (ItemCategory) entity;
        ChatActivity.showGroup(getContext(), itemCategory.getId());
      }
    }

    @Override public void onRightMenuClick(MultiItemEntity entity) {
      Timber.i("点击删除--" + entity.toString());
      ItemUser user = (ItemUser) entity;
      presenter.deleteUser(user.getId());
    }
  };

  @Override public void setPresenter(UserContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void showData(final List<MultiItemEntity> entityList) {
    Timber.e("接收");
    handler.post(new Runnable() {
      @Override public void run() {
        adapter.setNewData(entityList);
      }
    });
  }

  @Override public void showText(int stringId) {
    ToastUtil.show(stringId);
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }
}
